package com.demo.project_template.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.demo.project_template.entity.Product;
import com.demo.project_template.entity.ProductCategory;
import com.demo.project_template.mapper.ProductCategoryMapper;
import com.demo.project_template.mapper.ProductMapper;
import com.demo.project_template.service.ProductCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品类目表，类目和商品(spu)是一对多关系，类目与品牌是多对多关系 服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2023-06-19 15:56:44
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {
    @Resource
    private ProductCategoryMapper productCategoryMapper;
    @Resource
    private ProductMapper productMapper;

    @Override
    @Transactional
    public void deleteProductCategory(Integer productCategoryId) {
        List<Product> products = productMapper.selectList(new LambdaUpdateWrapper<Product>().eq(Product::getProductCategoryId, productCategoryId));
        List<Integer> productIds = products.stream().map(Product::getProductId).collect(Collectors.toList());
        productMapper.deleteBatchIds(productIds);
        productCategoryMapper.deleteById(productCategoryId);
    }
}
